//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : ipset.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "ipset.h"
#include "ui_ipset.h"
#include <QScreen>
#include <QButtonGroup>

static int g_screenWidth;
static int g_screenHeight;
static int g_openFlag = 1;           // 0:Ethernet  off  1:Ethernet  on
static int g_staticIpUpFlag;         // 0:No static network is created  1:Creating a Static Network
static QStringList g_ethernetPort;
static QScreen *g_screen;
static QLabel *g_swicthLabel1;
static QLabel *g_swicthLabel2;
static QHBoxLayout *g_horLayout;

ipset::ipset(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ipset)
{
    ui->setupUi(this);
    ui->textEdit->verticalScrollBar()->setStyleSheet("QScrollBar{width:25px;}");
    ui->stackedWidget->setCurrentIndex(0);
    g_screen = qApp->primaryScreen();
    g_screenWidth = g_screen->size().width();
    g_screenHeight = g_screen->size().height();
    QButtonGroup *testButtonGroup = new QButtonGroup(this);
    testButtonGroup->setExclusive(false);
    testButtonGroup->addButton(ui->rdbtn_staticIP,0);
    testButtonGroup->addButton(ui->rdbtn_autoIP,1);
    testButtonGroup->addButton(ui->rdbtn_autoDns,2);
    testButtonGroup->addButton(ui->rdbtn_setDns,3);

    QRegExp a("\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b");  //IP regular expression
    ui->ipAddrLineEdit->setValidator(new QRegExpValidator(a,this));
    ui->gatewaylineEdit->setValidator(new QRegExpValidator(a,this));
    ui->edit_preDns->setValidator(new QRegExpValidator(a,this));
    ui->edit_altDns->setValidator(new QRegExpValidator(a,this));
    ui->masklineEdit->setPlaceholderText("1-32");

    ui->widget_ip->setEnabled(false);
    ui->widget_ip->setStyleSheet("padding: 6px;"
                                 "background-color: rgb(217, 217, 217,30);"
                                 "border:1px solid gray;");
    ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                    "padding: 6px; "
                                    "background-color:rgba(255, 255, 255, 80);"
                                    "background-color: rgb(217, 217, 217,30);"
                                    "border:1px solid gray;"
                                    "color: rgb(186, 189, 182);");
    ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                      "padding: 6px; "
                                      "background-color:rgba(255, 255, 255, 80);"
                                      "background-color: rgb(217, 217, 217,30);"
                                      "border:1px solid gray;"
                                      "color: rgb(186, 189, 182);");
    ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                       "padding: 6px; "
                                       "background-color:rgba(255, 255, 255, 80);"
                                       "background-color: rgb(217, 217, 217,30);"
                                       "border:1px solid gray;"
                                       "color: rgb(186, 189, 182);");
    ui->ip_Switch->setCheckedColor(QColor(100, 225, 100, 150));
    ui->rdbtn_autoIP->setChecked(true);
    ui->widget_dns->setEnabled(false);
    ui->widget_dns->setStyleSheet("padding: 6px;"
                                  "background-color: rgb(217, 217, 217,30);"
                                  "border:1px solid gray;");
    ui->rdbtn_autoDns->setChecked(true);

    setIpText();
    setDnsText();
    setSwitchText();
    getEthernetPort();
    setIpFont();
    QString networkInfo = getNetworkInfo(g_ethernetPort);
    ui->textEdit->setText(networkInfo);
    ui->stackedWidget->setCurrentIndex(0);
    connect(ui->ip_Switch,SIGNAL(toggled(bool)),this,SLOT(switch_change_flag(bool)));
    ui->scrollArea->viewport()->setStyleSheet(".QWidget{background-color:transparent;border-color: rgba(255, 225, 255, 0);}");
    ui->scrollArea->setFrameShape(QFrame::NoFrame);
}

ipset::~ipset()
{
    delete ui;
    delete g_swicthLabel1;
    delete g_swicthLabel2;
    delete g_horLayout;
}

void ipset::languageReload()
{
    ui->retranslateUi(this);
    int flag = ui->ip_Switch->isToggled();
    if(flag == 1)
    ui->ip_Switch->setToggle(true);
    else
    ui->ip_Switch->setToggle(false);
}

void ipset::on_btn_ret_clicked()
{
    emit ipset_back_msg();
    ui->textEdit->setText("");
}

void ipset::on_btn_ipShow_clicked()
{
    if(g_openFlag == 0)
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Please open the ethernet!"),
                         0,this);
        mesg.addButton(tr("ok"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
        return;
    }

    QString networkInfo = getNetworkInfo(g_ethernetPort);
    ui->textEdit->setText(networkInfo);
    ui->stackedWidget->setCurrentIndex(0);
}

void ipset::on_btn_setIp_clicked()
{
    if(g_openFlag == 0)
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Please open the ethernet!"),
                         0,this);
        mesg.addButton(tr("ok"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
        return;
    }
    ui->stackedWidget->setCurrentIndex(1);
}

void ipset::on_btn_cancel_clicked()
{
    ui->widget_ip->setEnabled(false);
    ui->widget_ip->setStyleSheet("padding: 6px;"
                                 "background-color: rgb(217, 217, 217,30);"
                                 "border:1px solid gray;");
    ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                    "padding: 6px; "
                                    "background-color:rgba(255, 255, 255, 80);"
                                    "background-color: rgb(217, 217, 217,30);"
                                    "border:1px solid gray;"
                                    "color: rgb(186, 189, 182);");
    ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                      "padding: 6px; "
                                      "background-color:rgba(255, 255, 255, 80);"
                                      "background-color: rgb(217, 217, 217,30);"
                                      "border:1px solid gray;"
                                      "color: rgb(186, 189, 182);");
    ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                       "padding: 6px; "
                                       "background-color:rgba(255, 255, 255, 80);"
                                       "background-color: rgb(217, 217, 217,30);"
                                       "border:1px solid gray;"
                                       "color: rgb(186, 189, 182);");
    ui->ip_Switch->setCheckedColor(QColor(100, 225, 100, 150));
    ui->rdbtn_autoIP->setChecked(true);
    ui->widget_dns->setEnabled(false);
    ui->widget_dns->setStyleSheet("padding: 6px;"
                                  "background-color: rgb(217, 217, 217,30);"
                                  "border:1px solid gray;");
    ui->rdbtn_autoDns->setChecked(true);
    ui->rdbtn_setDns->setChecked(false);
    ui->rdbtn_staticIP->setChecked(false);
    ui->edit_altDns->clear();
    ui->edit_preDns->clear();
    ui->masklineEdit->clear();
    ui->gatewaylineEdit->clear();
    ui->ipAddrLineEdit->clear();
    setIpText();
    setDnsText();
}

void ipset::setAutoIp()
{
    if(g_openFlag == 0)
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Please open the ethernet!"),
                         0,this);
        mesg.addButton(tr("ok"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
        return;
    }
    if(ui->rdbtn_setDns->isChecked())
    {
        if((ui->edit_preDns->text().length() < 7)||(ui->edit_altDns->text().length() < 7))
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("Format error, please re-enter."),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
            return;
        }
    }
    ui->widget_ip->setEnabled(false);
    ui->widget_ip->setStyleSheet("padding: 6px;"
                                 "background-color: rgb(217, 217, 217,30);"
                                 "border:1px solid gray;");
    ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                    "padding: 6px; "
                                    "background-color:rgba(255, 255, 255, 80);"
                                    "background-color: rgb(217, 217, 217,30);"
                                    "border:1px solid gray;""color: rgb(186, 189, 182);");
    ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                      "padding: 6px; "
                                      "background-color:rgba(255, 255, 255, 80);"
                                      "background-color: rgb(217, 217, 217,30);"
                                      "border:1px solid gray;""color: rgb(186, 189, 182);");
    ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                       "padding: 6px; "
                                       "background-color:rgba(255, 255, 255, 80);"
                                       "background-color: rgb(217, 217, 217,30);"
                                       "border:1px solid gray;""color: rgb(186, 189, 182);");

    if(false == isStaticIpExist())
    {
        if(!g_ethernetPort.isEmpty())
        {
            QString strCmd = QString("nmcli con show --active |grep 'ethernet' -wc");
            QString strResult = executeLinuxCmd(strCmd);
            if(strResult.toInt() == g_ethernetPort.size())
            {
                QMessageBox mesg(QMessageBox::Information,
                                 tr("QMessageBox::information()"),
                                 tr("Set auto ip succeeded!"),
                                 0,this);
                mesg.addButton(tr("OK"),QMessageBox::YesRole);
                mesg.move(g_screenWidth/3,g_screenHeight/3);
                mesg.exec();

                QString networkInfo = getNetworkInfo(g_ethernetPort);
                ui->textEdit->setText(networkInfo);
                ui->stackedWidget->setCurrentIndex(0);
                networkEnable(g_ethernetPort,true);
            }
            else
            {
                for(int i = 0;i<g_ethernetPort.size();i++)
                {
                    QString strCmd = QString("nmcli con show --active |grep %1").arg(g_ethernetPort.at(i));
                    QString strResult = executeLinuxCmd(strCmd);
                    if(strResult.isEmpty())
                    {
                        strCmd = QString("ifconfig %1 up").arg(g_ethernetPort.at(i));
                        executeLinuxCmd(strCmd);
                    }
                }
                strCmd = QString("nmcli con show --active |grep 'ethernet' -wc");
                strResult = executeLinuxCmd(strCmd);

                if(strResult.toInt() > 0)
                {
                    QMessageBox mesg(QMessageBox::Information,
                                     tr("QMessageBox::information()"),
                                     tr("Set auto ip succeeded!"),
                                     0,this);
                    mesg.addButton(tr("OK"),QMessageBox::YesRole);
                    mesg.move(g_screenWidth/3,g_screenHeight/3);
                    mesg.exec();
                    QString networkInfo = getNetworkInfo(g_ethernetPort);
                    ui->textEdit->setText(networkInfo);
                    ui->stackedWidget->setCurrentIndex(0);
                    ui->ipAddrLineEdit->clear();
                    ui->gatewaylineEdit->clear();
                    ui->masklineEdit->clear();
                    g_staticIpUpFlag = 0;
                    networkEnable(g_ethernetPort,true);
                }
                else
                {
                    QMessageBox mesg(QMessageBox::Information,
                                     tr("QMessageBox::information()"),
                                     tr("set auto ip failed"),
                                     0,this);
                    mesg.addButton(tr("OK"),QMessageBox::YesRole);
                    mesg.move(g_screenWidth/3,g_screenHeight/3);
                    mesg.exec();
                }
            }
        }
    }
    else
    {
        bool result = deleteStaticIp();          // Delete static ip
        if(result == true)
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("Set auto ip succeeded!"),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
            QString networkInfo = getNetworkInfo(g_ethernetPort);
            ui->textEdit->setText(networkInfo);
            ui->stackedWidget->setCurrentIndex(0);
            ui->ipAddrLineEdit->clear();
            ui->gatewaylineEdit->clear();
            ui->masklineEdit->clear();
            g_staticIpUpFlag = 0;
            networkEnable(g_ethernetPort,true);
        }
        else
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("set auto ip failed"),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
        }
    }
    if(ui->rdbtn_setDns->isChecked())
    {
        QStringList listDns = g_database.tableShow("dnsAuto");
        if(!listDns.isEmpty())
        {
            for(int i =0; i<listDns.length();i++)
            {
                QString str = QString("nmcli con modify 'Wired connection 1' -ipv4.dns %1").arg(listDns[i]);
                executeLinuxCmd(str);
                str = QString("nmcli connection up 'Wired connection 1'");
                executeLinuxCmd(str);
                g_database.deleteTableName("dnsAuto",listDns.at(i));
            }
        }

        QString strDns1 = ui->edit_preDns->text();
        QString str = QString("nmcli con modify 'Wired connection 1' ipv4.dns %1").arg(strDns1);
        executeLinuxCmd(str);
        str = QString("nmcli connection up 'Wired connection 1'");
        executeLinuxCmd(str);
        QString strDns2 = ui->edit_altDns->text();
        str = QString("nmcli con modify 'Wired connection 1' +ipv4.dns %1").arg(strDns2);
        executeLinuxCmd(str);
        str = QString("nmcli connection up 'Wired connection 1'");
        executeLinuxCmd(str);

        g_database.insertTableOne("dnsAuto",strDns1);
        g_database.insertTableOne("dnsAuto",strDns2);
        g_database.tableDebug("dnsAuto");
    }
    if(ui->rdbtn_autoDns->isChecked())
    {
        QStringList listDns = g_database.tableShow("dnsAuto");
        if(!listDns.isEmpty())
        {
            for(int i =0; i<listDns.length();i++)
            {
                QString str = QString("nmcli con modify 'Wired connection 1' -ipv4.dns %1").arg(listDns.at(i));
                executeLinuxCmd(str);
                str = QString("nmcli connection up 'Wired connection 1'");
                executeLinuxCmd(str);
                g_database.deleteTableName("dnsAuto",listDns.at(i));
            }
        }
    }
}

void ipset::setStaticIp()
{
    bool result = false;
    QString networkInfo;
    if(ui->cb_name->currentText().isEmpty() || ui->ipAddrLineEdit->text().isEmpty() || ui->gatewaylineEdit->text().isEmpty() || ui->masklineEdit->text().isEmpty())
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Please complete the information"),
                         0,this);
        mesg.addButton(tr("OK"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
        return;
    }

    if(ui->rdbtn_setDns->isChecked())
    {

        if((ui->edit_preDns->text().length() < 7)||(ui->edit_altDns->text().length() < 7)||ui->edit_preDns->text().isEmpty()||ui->edit_altDns->text().isEmpty())
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("Format error, please re-enter."),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
            return;
        }
    }

    QMessageBox mesg(QMessageBox::Question,
                     tr("QMessageBox::question()"),
                     tr("Do you want to set it to current IP?"),
                     0,this);
    QPushButton *yesButton = mesg.addButton(tr("Yes"), QMessageBox::ActionRole);
    QPushButton *noButton = mesg.addButton(tr("No"),QMessageBox::ActionRole);
    mesg.move(g_screenWidth/3,g_screenHeight/3);
    mesg.exec();

    if(mesg.clickedButton() == yesButton)
    {
        if((ui->ipAddrLineEdit->text().length() <= 9) || (ui->gatewaylineEdit->text().length() <= 9) || (ui->ipAddrLineEdit->text().length() <= 9) || (ui->gatewaylineEdit->text().length() <= 9))
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("Format error, please re-enter."),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
            return;
        }
        int mask = ui->masklineEdit->text().toInt();
        if((mask > 32)||(mask < 1))
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("Please enter the correct subnet mask."),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
            return;
        }
         result = addStaticIp(ui->cb_name->currentText(),ui->ipAddrLineEdit->text(),
        ui->masklineEdit->text(),ui->gatewaylineEdit->text());
        if(result == true)
        {
            networkInfo = getNetworkInfo(g_ethernetPort);
            ui->textEdit->setText(networkInfo);
            ui->stackedWidget->setCurrentIndex(0);
            if(g_staticIpUpFlag == 0)
            {
                g_staticIpUpFlag = 1;
            }
        }
        else
        {
            QMessageBox mesg(QMessageBox::Information,
                             tr("QMessageBox::information()"),
                             tr("set static ip failed"),
                             0,this);
            mesg.addButton(tr("OK"),QMessageBox::YesRole);
            mesg.move(g_screenWidth/3,g_screenHeight/3);
            mesg.exec();
        }
    }

    if(ui->rdbtn_setDns->isChecked())
    {
        QStringList listDns = g_database.tableShow("dnsStatic");
        if(!listDns.isEmpty())
        {
            for(int i =0; i<listDns.length();i++)
            {
                QString str = QString("nmcli con modify static_ip -ipv4.dns %1").arg(listDns[i]);
                executeLinuxCmd(str);
                str = QString("nmcli connection up static_ip");
                executeLinuxCmd(str);
                g_database.deleteTableName("dnsStatic",listDns.at(i));
            }
        }
        QString strDns1 = ui->edit_preDns->text();
        QString str = QString("nmcli con modify static_ip ipv4.dns %1").arg(strDns1);
        executeLinuxCmd(str);
        str = QString("nmcli connection up static_ip");
        executeLinuxCmd(str);
        QString strDns2 = ui->edit_altDns->text();
        str = QString("nmcli con modify static_ip +ipv4.dns %1").arg(strDns2);
        executeLinuxCmd(str);
        str = QString("nmcli connection up static_ip");
        executeLinuxCmd(str);

        g_database.insertTableOne("dnsStatic",strDns1);
        g_database.insertTableOne("dnsStatic",strDns2);
    }
    if(ui->rdbtn_autoDns->isChecked())
    {
        QStringList listDns = g_database.tableShow("dnsStatic");
        if(!listDns.isEmpty())
        {
            for(int i =0; i<listDns.length();i++)
            {
                QString str = QString("nmcli con modify static_ip -ipv4.dns %1").arg(listDns.at(i));
                executeLinuxCmd(str);
                str = QString("nmcli connection up static_ip");
                executeLinuxCmd(str);
                g_database.deleteTableName("dnsStatic",listDns.at(i));
            }
        }
    }
}

void ipset::setIpFont()
{
    qreal realX = g_screen->physicalDotsPerInchX();
    qreal realWidth = g_screenWidth / realX * 2.54;
    QFont font;
    if(realWidth < 15)
    {
        font.setPointSize(12);
    }
    else if (realWidth < 18)
    {
        font.setPointSize(14);
    }
    else
    {
        font.setPointSize(17);
    }
    ui->btn_ipShow->setFont(font);
    ui->btn_cancel->setFont(font);
    ui->btn_setIp->setFont(font);
    ui->ipAddrLineEdit->setFont(font);
    ui->lbl_gatway->setFont(font);
    ui->lbl_subnetMask->setFont(font);
    ui->textEdit->setFont(font);
    ui->btn_ok->setFont(font);
    ui->lbl_ethernet->setFont(font);
    ui->lbl_ipAddress->setFont(font);
    ui->lbl_ipName->setFont(font);
    ui->gatewaylineEdit->setFont(font);
    ui->masklineEdit->setFont(font);
    ui->cb_name->setFont(font);\
    ui->rdbtn_autoIP->setFont(font);
    ui->rdbtn_staticIP->setFont(font);
    ui->lbl_preDnsServer->setFont(font);
    ui->edit_preDns->setFont(font);
    ui->rdbtn_autoDns->setFont(font);
    ui->rdbtn_setDns->setFont(font);
    ui->edit_altDns->setFont(font);
    ui->lbl_altDnsServer->setFont(font);
    g_swicthLabel1->setFont(font);
    g_swicthLabel2->setFont(font);
    font.setLetterSpacing(QFont::PercentageSpacing,110);         // Word space
    font.setPointSize(18);
    font.setBold(true);
    ui->lbl_ipOff->setFont(font);
}

void ipset::setIpText()
{
    if(true == isStaticIpExist())
    {
        QString strCmd = QString("nmcli con show --active | grep static| awk '{print $4}'");
        QString strEthrenetPort = executeLinuxCmd(strCmd);
        strEthrenetPort = strEthrenetPort.trimmed();
        strCmd = QString("ifconfig %1 | awk '{print $2}'|awk 'NR==2'").arg(strEthrenetPort);
        QString strResult = executeLinuxCmd(strCmd);
        strResult.remove("\n");
        ui->ipAddrLineEdit->setText(strResult);
        strCmd = QString("ip route show | grep %1").arg(strEthrenetPort);
        strResult = executeLinuxCmd(strCmd);
        strResult = strResult.section(" ",2,2);
        ui->gatewaylineEdit->setText(strResult);

        ui->widget_ip->setEnabled(true);
        ui->widget_ip->setStyleSheet("padding: 6px;"
                                     "background-color:rgba(255, 255, 255, 80);"
                                     "border:1px solid gray;");
        ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                        "padding: 6px; "
                                        "background-color:rgba(255, 255, 255, 80);"
                                        "background-color: rgb(217, 217, 217,30);"
                                        "border:1px solid gray;"
                                        "color: rgb(0, 0, 0);");
        ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                          "padding: 6px; "
                                          "background-color:rgba(255, 255, 255, 80);"
                                          "background-color: rgb(217, 217, 217,30);"
                                          "border:1px solid gray;"
                                          "color: rgb(0, 0, 0);");
        ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                           "padding: 6px; "
                                           "background-color:rgba(255, 255, 255, 80);"
                                           "background-color: rgb(217, 217, 217,30);"
                                           "border:1px solid gray;"
                                           "color: rgb(0, 0, 0);");
        ui->btn_ok->setText(tr("OK"));
        ui->rdbtn_staticIP->setChecked(true);
        ui->rdbtn_autoIP->setChecked(false);
    }
}

void ipset::switch_change_flag(bool flag)
{
    flag = ui->ip_Switch->isToggled();
    if(flag == 1)
    {
        g_openFlag = 1;
        networkEnable(g_ethernetPort,true);
        QString networkInfo = getNetworkInfo(g_ethernetPort);
        ui->textEdit->setText(networkInfo);
        ui->stackedWidget->setCurrentIndex(0);
        g_swicthLabel2->setFixedWidth(ui->ip_Switch->height());
        g_swicthLabel2->clear();
        g_swicthLabel1->setMinimumSize(0,0);
        g_swicthLabel1->setText(tr("off"));
        g_swicthLabel1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
        g_swicthLabel1->setStyleSheet("color: rgba(0, 0, 0,180);");
        ui->btn_ipShow->setStyleSheet("background-color: rgba(100, 225, 100, 120);"
                                      "border-style: outset;"
                                      "color:rgba(0, 0, 0, 100)");
        ui->btn_setIp->setStyleSheet("background-color: rgba(100, 225, 100, 120);"
                                      "border-style: outset;"
                                      "color:rgba(0, 0, 0, 100)");
    }
    else
    {
        g_openFlag = 0;
        networkEnable(g_ethernetPort,false);
        ui->textEdit->setText("");
        ui->stackedWidget->setCurrentIndex(2);
        g_swicthLabel1->setFixedWidth(ui->ip_Switch->height());
        g_swicthLabel2->setText(tr("on"));
        g_swicthLabel1->clear();
        g_swicthLabel2->setMinimumSize(0,0);
        g_swicthLabel2->setStyleSheet("color: rgba(255, 255, 255,200);");
        ui->btn_ipShow->setStyleSheet("background-color: rgba(211, 215, 207,160);"
                                      "border-style: outset;"
                                      "border-width:  2px;"
                                      "color:rgba(186, 189, 182);");
        ui->btn_setIp->setStyleSheet("background-color: rgba(211, 215, 207,160);"
                                      "border-style: outset;"
                                      "border-width:  2px;"
                                      "color:rgba(186, 189, 182);");
    }
}

void ipset::setSwitchText()
{
    g_swicthLabel1 = new QLabel(ui->ip_Switch);
    g_swicthLabel2 = new QLabel(ui->ip_Switch);
    g_horLayout = new QHBoxLayout();
    g_horLayout->setContentsMargins(0,0,0,0);
    g_swicthLabel1->setText(tr("off"));
    g_horLayout->addWidget(g_swicthLabel1);
    g_horLayout->addWidget(g_swicthLabel2);
    g_swicthLabel1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    g_swicthLabel1->setStyleSheet("color: rgba(0, 0, 0,180);");
    ui->ip_Switch->setLayout(g_horLayout);
}

void ipset::getEthernetPort()
{
    ui->cb_name->clear();
    QString str = scanEthernetPort();
    QStringList list = str.split("\n");
    list.removeAll("");
    for(int i = 0;i<list.size();i++)
    {
        str = list[i];
        str = str.remove(4,str.length());
        ui->cb_name->addItem(str);
        g_ethernetPort << str;
    }
}

void ipset::on_btn_ok_clicked()
{

    if(ui->rdbtn_autoIP->isChecked())
    {
        setAutoIp();
    }

    else if(ui->rdbtn_staticIP->isChecked())
    {
        setStaticIp();
    }
}

void ipset::on_rdbtn_staticIP_clicked()
{
    ui->widget_ip->setEnabled(true);
    ui->widget_ip->setStyleSheet("padding: 6px;"
                                 "background-color:rgba(255, 255, 255, 60);"
                                 "border:1px solid gray;");
    ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                    "padding: 6px; "
                                    "background-color:rgba(255, 255, 255, 80);"
                                    "background-color: rgb(217, 217, 217,30);"
                                    "border:1px solid gray;"
                                    "color: rgb(0, 0, 0);");
    ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                      "padding: 6px; "
                                      "background-color:rgba(255, 255, 255, 80);"
                                      "background-color: rgb(217, 217, 217,30);"
                                      "border:1px solid gray;"
                                      "color: rgb(0, 0, 0);");
    ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                       "padding: 6px; "
                                       "background-color:rgba(255, 255, 255, 80);"
                                       "background-color: rgb(217, 217, 217,30);"
                                       "border:1px solid gray;"
                                       "color: rgb(0, 0, 0);");
    ui->rdbtn_autoIP->setChecked(false);
    ui->rdbtn_autoDns->setChecked(false);
    ui->rdbtn_setDns->setChecked(true);
    ui->widget_dns->setEnabled(true);
    ui->widget_dns->setStyleSheet("padding: 6px;"
                                  "background-color:rgba(255, 255, 255, 60);"
                                  "border:1px solid gray;");
}

void ipset::on_rdbtn_autoIP_clicked()
{
    ui->widget_ip->setEnabled(false);
    ui->widget_ip->setStyleSheet("padding: 6px;"
                                 "background-color: rgb(217, 217, 217,30);"
                                 "border:1px solid gray;");
    ui->masklineEdit->setStyleSheet("border-radius: 12px; "
                                    "padding: 6px; "
                                    "background-color:rgba(255, 255, 255, 80);"
                                    "background-color: rgb(217, 217, 217,30);"
                                    "border:1px solid gray;"
                                    "color: rgb(186, 189, 182);");
    ui->rdbtn_staticIP->setChecked(false);
    ui->ipAddrLineEdit->setStyleSheet("border-radius: 12px; "
                                      "padding: 6px; "
                                      "background-color:rgba(255, 255, 255, 80);"
                                      "background-color: rgb(217, 217, 217,30);"
                                      "border:1px solid gray;"
                                      "color: rgb(186, 189, 182);");
    ui->gatewaylineEdit->setStyleSheet("border-radius: 12px; "
                                       "padding: 6px; "
                                       "background-color:rgba(255, 255, 255, 80);"
                                       "background-color: rgb(217, 217, 217,30);"
                                       "border:1px solid gray;"
                                       "color: rgb(186, 189, 182);");
}

void ipset::on_rdbtn_autoDns_clicked()
{
    ui->widget_dns->setEnabled(false);
    ui->widget_dns->setStyleSheet("padding: 6px;"
                                  "background-color: rgb(217, 217, 217,30);"
                                  "border:1px solid gray;");
    ui->rdbtn_setDns->setChecked(false);
    ui->edit_preDns->clear();
    ui->edit_altDns->clear();
}

void ipset::on_rdbtn_setDns_clicked()
{
    ui->widget_dns->setEnabled(true);
    ui->widget_dns->setStyleSheet("padding: 6px;"
                                  "background-color:rgba(255, 255, 255, 60);"
                                  "border:1px solid gray;");
    ui->rdbtn_autoDns->setChecked(false);
    QStringList listDns;
    if(ui->rdbtn_autoIP->isChecked())
      listDns = g_database.tableShow("dnsAuto");
    if(ui->rdbtn_staticIP->isChecked())
      listDns = g_database.tableShow("dnsStatic");
    if(!listDns.isEmpty())
    {
        ui->edit_preDns->setText(listDns[0]);
        ui->edit_altDns->setText(listDns[1]);
    }
}

void ipset::setDnsText()
{
    QStringList listDns;
    if(ui->rdbtn_autoIP->isChecked())
      listDns = g_database.tableShow("dnsAuto");
    if(ui->rdbtn_staticIP->isChecked())
      listDns = g_database.tableShow("dnsStatic");
    if(!listDns.isEmpty())
    {
        ui->edit_preDns->setText(listDns[0]);
        ui->edit_altDns->setText(listDns[1]);
        ui->rdbtn_setDns->setChecked(true);
        ui->rdbtn_autoDns->setChecked(false);
        ui->widget_dns->setEnabled(true);
        ui->widget_dns->setStyleSheet("padding: 6px;"
                                      "background-color:rgba(255, 255, 255, 60);"
                                      "border:1px solid gray;");
    }
    else
    {
        ui->rdbtn_setDns->setChecked(false);
        ui->rdbtn_autoDns->setChecked(true);
    }
}

void ipset::showEvent(QShowEvent *event)
{
    g_swicthLabel2->setFixedWidth(ui->ip_Switch->height());
}
